using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    public class Totem : Unit
    {
        public Unit summoner;
        public int duration;
        
        public override BattleUnitStatistics statistics => this.summoner.statistics;
        
        protected override void OnDispose()
        {
            this.battle = null;
            this.summoner = null;
        }

        public override IPOD GeneratePOD()
        {
            var rPod = new BattleUnitViewPOD()
            {
                guid = this.GUID,
                unitType = (int)EUnitType.Totem,
                pos = this.pos.ToPOD(),
            };
            return rPod;
        }

        public override void SetPos(LVector2 rNewPos, LVector2 rForward, List<LVector2> path = null, bool checkMoveRange = true)
        {
            this.mPos = rNewPos;
        }
        
        public override void OnRoundStart()
        {
            this.duration--;
            if (this.duration < 0)
            {
                this.battle.playCtrl.unitCtrl.RemoveUnit(this);
                return;
            }
            var rComp = this.GetComponent<TotemComponent>(ETComponentType.TotemComponent);
            var rTotemResp = ClassPool.Get<TotemResponse>();
            rTotemResp.SetUnit(this, this.summoner);
            if (rComp.TryActive(SkillEffective_Totem_ConfigData.EActiveType.RoundStart, rTotemResp, out var rResp))
            {
                var rResultCtrl = this.summoner.battle.playCtrl.resultCtrl;
                rResultCtrl.Add(BattleUpdateFactory.CreateSkillEffectiveEnd(this.GUID, rResp, 0));
            }
            else
            {
                ClassPool.Put(rTotemResp);
            }
        }

        public override void OnRoundEnd()
        {
            var rComp = this.GetComponent<TotemComponent>(ETComponentType.TotemComponent);
            var rTotemResp = ClassPool.Get<TotemResponse>();
            rTotemResp.SetUnit(this, this.summoner);
            if (rComp.TryActive(SkillEffective_Totem_ConfigData.EActiveType.RoundEnd, rTotemResp, out var rResp))
            {
                var rResultCtrl = this.summoner.battle.playCtrl.resultCtrl;
                rResultCtrl.Add(BattleUpdateFactory.CreateSkillEffectiveEnd(this.GUID, rResp, 0));
            }
            else
            {
                ClassPool.Put(rTotemResp);
            }
        }
        
#region Formula Func

        public override AttributeProperty GetAttr()
        {
            return this.summoner.GetComponent<AttributeComponent>(ETComponentType.AttributeComponent).attr;
        }

        public override int GetBuffStack(int buffID)
        {
            var nStack = 0;
            var rBuffComp = this.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
            for (int i = 0; i < rBuffComp.buffList.Count; i++)
            {
                var rBuff = rBuffComp.buffList[i];
                if (rBuff.buffID == buffID)
                {
                    nStack = rBuff.stack;
                    break;
                }
            }
            return nStack;
        }

        public override T GetCustomProperty<T>(ECustomProperty propertyType)
        {
            return this.summoner.GetComponent<CustomPropertyComponent>(ETComponentType.CustomPropertyComponent).GetProperty<T>(propertyType);
        }

#endregion
    }
}